﻿using Api1.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace Api1.Service
{
    /// <summary>
    /// 东夏接口服务类
    /// </summary>
    public class DXApiService
    {
        public static DXApiService instance;
        /// <summary>
        /// 会员信息接口访问地址
        /// </summary>
        public static String url = OperateIniFile.ReadIniData("DongXia", "url", "http://cddgwe.fwsoft.cn/Users/OpenDoor.asmx");
        public static String url_get_contract_info =url+ "/GetMemberInfo?shopname=&shopid={0}&loginname=&loginid={1}&password={2}&cardindex={3}&projectname=&mode=only_out";//获取合同号
        public static String url_check_in = url+ "/CheckCardindex?shopname=&shopid={0}&loginname=&loginid={1}&password={2}&cardindex={3}&projectname=&mode=only_in&brushnum=2&repeat=yes&billtype=new";//入场
        public static String url_check_out= url+ "/CheckCardindex?shopname=&shopid={0}&loginname=&loginid={1}&password={2}&cardindex={3}&projectname=&mode=only_out&brushnum=2&repeat=yes&billtype=new";//出场
        private static String shopid= OperateIniFile.ReadIniData("DongXia", "shopid", "1");
        private static String loginid = OperateIniFile.ReadIniData("DongXia", "loginid", "3");
        private static String password = OperateIniFile.ReadIniData("DongXia", "password", "123456");
        public DXApiService() {
        }
        public static DXApiService GetInstance() {
            if (instance == null) {
                instance = new DXApiService();
            }
            return instance;
        }

        /// <summary>
        ///  根据卡号获取合同编号
        /// </summary>
        /// <param name="Card_No"></param>
        /// <returns></returns>
        public Dictionary<String, Object> GetContractInfo(String Card_No) {
            ///参数
            String link = String.Format(url_get_contract_info,shopid,loginid,password,Card_No);
            ///字符串结果 去掉 xml 标签
            String  result = HttpClient.GetNative(link);
            if (String.IsNullOrWhiteSpace(result))
            {
                return HttpClient.Error("配置错误或者卡号不存在dx");
            }
            try {
                result = result.Replace("\n","").Replace("\r","").Replace("xmlns=\"http://tempuri.org/\"","");
                using (var sr=new StringReader(result)) {
                    XmlReader xmlReader = XmlReader.Create(sr);
                    XmlSerializer xmlSerial = new XmlSerializer(typeof(DataTable));
                    DataTable table1 = xmlSerial.Deserialize(xmlReader) as DataTable;
                    if (table1 != null)
                    {
                      //  <Table diffgr:id="Table1" msdata:rowOrder="0">
                      //  <cardFaceIndex>000001</cardFaceIndex>
                      //  <cardIndex>000001</cardIndex>
                      //  <activeDate>2016-04-10T19:58:42+08:00</activeDate>
                      //  <endDate>2019-04-10T19:58:42+08:00</endDate>
                      //  <cardStatus>激活</cardStatus>
                      //  <cardName>游泳健身二年卡</cardName>
                      //  <cardTime>游泳健身:-4</cardTime>
                      //  <name>钟刚</name>
                      //  <phone>13890060666</phone>
                      //</Table>
                        String tableName = table1.TableName;
                        if (tableName.Equals("Table"))
                        {//获取会员信息成功
                            Dictionary<String, String> member = new Dictionary<string, string>();
                            member.Add("ContractID",table1.Rows[0][1].ToString()+ table1.Rows[0][8].ToString());
                            member.Add("MemberID", table1.Rows[0][1].ToString() + table1.Rows[0][8].ToString());
                            member.Add("MemberName", table1.Rows[0][7].ToString());
                            member.Add("Mobile", table1.Rows[0][8].ToString());
                            member.Add("CardNo", table1.Rows[0][1].ToString());
                            member.Add("StartDate", table1.Rows[0][2].ToString());
                            member.Add("EndDate", table1.Rows[0][3].ToString());
                            member.Add("ContractStatus","1");
                            member.Add("MembershipType", "2");
                            member.Add("MembershipTypeName", table1.Rows[0][5].ToString());
                            member.Add("SurplusNum", "0");
                            return HttpClient.Success("成功",member);
                        }
                        else {//获取会员信息失败
                            return HttpClient.Error(tableName);
                        }
                    }
                    else {
                        return HttpClient.Error("接口错误！");
                    }
                }
                
                //return HttpClient.Success("成功",null);
            } catch (Exception e) {
                 ApiServiceImpl.WriteLog(typeof(AKApiService),e);                
                 return HttpClient.Error("返回模板解析错误");
            }            
        }
        /// <summary>
        /// 入场操作
        /// </summary>
        /// <param name="CardNO"></param>
        /// <returns></returns>
        public Dictionary<String, Object> CheckIn(String Card_NO) {
            String link = String.Format(url_check_in, shopid, loginid, password, Card_NO);
            ///字符串结果 去掉 xml 标签
            String result = HttpClient.GetNative(link);
            result = result.Replace("\n","").Replace("\r","").Replace("xmlns=\"http://tempuri.org/\"","");

            if (String.IsNullOrWhiteSpace(result))
            {
                return HttpClient.Error("卡号不存在或者配置错误dx");
            }
            else {
                using (var sr = new StringReader(result))
                {
                    XmlReader xmlReader = XmlReader.Create(sr);
                    XmlSerializer xmlSerial = new XmlSerializer(typeof(String));
                    String str = xmlSerial.Deserialize(xmlReader) as String;
                    if (str.Equals("ok"))
                    {
                        return HttpClient.Success("成功");
                    }
                    else {
                        return HttpClient.Error(str);
                    }
                }
            }
        }
        /// <summary>
        /// 出场操作
        /// </summary>
        /// <param name="CardNo"></param>
        /// <returns></returns>
        public Dictionary<String, Object> CheckOut(String CardNo) {
            String link = String.Format(url_check_out,shopid, loginid, password, CardNo);
            ///字符串结果 去掉 xml 标签
            String result = HttpClient.GetNative(link);
            result = result.Replace("\n", "").Replace("\r", "").Replace("xmlns=\"http://tempuri.org/\"", "");
            if (String.IsNullOrWhiteSpace(result))
            {
                return HttpClient.Error("卡号不存在或者配置错误dx");
            }
            else
            {
                using (var sr = new StringReader(result))
                {
                    XmlReader xmlReader = XmlReader.Create(sr);
                    XmlSerializer xmlSerial = new XmlSerializer(typeof(String));
                    String str = xmlSerial.Deserialize(xmlReader) as String;
                    if (str.Equals("ok"))
                    {
                        return HttpClient.Success("成功");
                    }
                    else
                    {
                        return HttpClient.Error(str);
                    }
                }
            }
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////
    }
}
